import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/Login.vue'
import home from '../views/home.vue'
import BookBorrowing from '../components/BookBorrowing.vue'
import BookReturn from '../components/BookReturn.vue'
import MyBorrowingRecords from '../components/MyBorrowingRecords.vue'
import ReaderAdmin from '../components/ReaderAdmin.vue'
import BorrowingRecords from '../components/BorrowingRecords.vue'
import BookAdmin from '../components/BookAdmin.vue'
import Bookrecall from '../components/Bookrecall.vue'
import Lendingranking from '../components/Lendingranking.vue'
import welcome from '../components/welcome.vue'
import FaceLogin from '@/components/facelogin.vue'
import PhotoCapture from '@/components/PhotoCapture.vue'
import SuccessPage from '@/components/SuccessPage.vue'
import {Message} from 'element-ui';
import BookScanReturn from '../components/BookScanReturn.vue'
import BookScanBorrowing from '../components/BookScanBorrowing.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: login
  },
  {
    path: '/facelogin',
    component: FaceLogin,
    meta: { requiresAuth: false }
  },
  {
    path: '/photocapture',
    component: PhotoCapture
  },
  {
    path: '/success',
    component: SuccessPage,
    meta: { requiresAuth: false }
  },
  {
    path: '/home',
    component: home,
    children:[
      {path:'/',component:welcome},//欢迎页
      {path:'/user/BookBorrowing',component:BookBorrowing},//图书借阅
      {path:'/user/BookReturn',component:BookReturn},//图书归还
      {path:'/user/MyBorrowingRecords',component:MyBorrowingRecords},//我的借阅记录
      {path:'/admin/ReaderAdmin',component:ReaderAdmin},//读者信息管理
      {path:'/admin/BorrowingRecords',component:BorrowingRecords},//借阅记录
      {path:'/admin/BookAdmin',component:BookAdmin},//图书管理
      {path:'/admin/Bookrecall',component:Bookrecall},//借出图书查询
      {path:'/admin/Lendingranking',component:Lendingranking},//书籍借出排行
      {path:'/user/BookScanReturn',component:BookScanReturn},//图书扫码归还
      {path:'/user/BookScanBorrowing',component:BookScanBorrowing},//图书扫码借书    
    ]
  }
]

const router = new VueRouter({
  routes
})

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // 修复登录路径的判断
  if(to.path === '/login' || to.path === '/facelogin' || to.path === '/photocapture') {
    return next()
  }
  
  // 获取token
  const tokenStr = localStorage.getItem('book_token')
  if(!tokenStr) {
    Message.error("你未登录！")
    return next('/login')
  }
  next()
})

//解决跳转同一路由报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router
